﻿/**
 * ImageEditor
 *
 * ImageEditor loads and edits images, and save the result as a JPEG image.
 *
 * ImageEditor is part of the sample application for the presentation on Flash ActionScript Cafe's 2nd conference.
 * 
 *
 * @author: Han Sanghun (http://hangunsworld.com, hanguns@gmail.com)
 * @created: 2008 05 16
 * @last modified: 2008 05 24
 */

package conf{
	
	import comp.cpMaker;
	
	import flash.display.*;
	import flash.events.*;
	
	import mx.controls.Alert;
	
	import vo.voImgInfo;
	
	public class ImageEditor extends MovieClip{
		
		//private var uploader:ImageUploader;
		public var handler:ImageHandler;
		
		public var cont_sp:Sprite;
		
		public var msg_sp:MsgViewer;
		
		public var _pa:cpMaker; // parentAplication
		
		public var _directPos:String;
		/**
		 * Constructor function
		 */
		public function ImageEditor(pa:*, directPos:String){
			_pa = pa.oCpMaker;
			_directPos = directPos;
			//uploader = new ImageUploader(this);
			
			handler = new ImageHandler(pa, directPos);
			addChild(handler);
			 
			cont_sp = new Sprite();
			if(directPos=="hgL") {
				_pa._ch.addChild(cont_sp);			 
				_pa._ch.bg_mc.addEventListener(MouseEvent.CLICK, bgClickListener);				
			} else if(directPos=="hgR"){
				_pa._chR.addChild(cont_sp);			 
				_pa._chR.bg_mc.addEventListener(MouseEvent.CLICK, bgClickListener);				
			}


			/*
			pa.load_btn.addEventListener(MouseEvent.CLICK, loadClickListener);
			
			// Load Image Button
			misc_mc.load_btn.addEventListener(MouseEvent.CLICK, loadClickListener);
			
			// Save Image Button
			misc_mc.save_btn.addEventListener(MouseEvent.CLICK, saveClickListener);
			
			setChildIndex(misc_mc, numChildren - 1);*/
			
		}// end Constructor function
		
		
		/**
		 * "bg_mc" 클릭하면, 이미지 선택 해제.
		 */
		private function bgClickListener(e:MouseEvent):void{
			
			handler.setTarget(null);
			
		}// end bgClickListener
		
		
		/**
		 * 파일 브라우저 열기.
		 */
		private function loadClickListener(e:MouseEvent):void{
			
			if(cont_sp.numChildren >= 5){
				Alert.show("No more than 5 images can be used.");
			}else{
				//uploader.browse();
			}
			
		}// end loadClickListener
		
		
		  
		/**
		 * 서버에 업로드된 이미지 불러오기.
		 *
		 * @param path 이미지 경로
		 */
		public function loadImage(path:String, _oSelectedObj:voImgInfo=null):void{
			//var loader:ImageHolder = new ImageHolder(SERVER_PATH + path, this);
			var loader:ImageHolder = new ImageHolder(path, this, _oSelectedObj);
//			loader.x = stage.stageWidth / 2;
//			loader.y = stage.stageHeight / 2;
	
			// 드래그로 신규 추가시... 
			if(_oSelectedObj==null) {
				var iMouseX:int = this.mouseX;
				var iMouseY:int = this.mouseY;
				
				loader.x = _pa.hgL.width / 2;
				loader.y = _pa.hgL.height / 2;
				
				_pa.setSelectedPage(cfv.ISEDITING, true);
			}
			// 좌측 클릭하여 로드할때
			else{
//				loader.x 			= _oSelectedObj.iPosX; 
//				loader.y 			= _oSelectedObj.iPosY;
//				loader.rotation 	= _oSelectedObj.iRotate;
				//handler.rotation	= _oSelectedObj.iRotate;
//				loader.width		= _oSelectedObj.iWidth;
//				loader.height		= _oSelectedObj.iHeight;
				
			}
//			loader.x = 0;
//			loader.y = 0;			
			cont_sp.addChild(loader);
			
			
		}// end loadImage
		
		
		/**
		 * 이미지 저장하기.
		 */
		private function saveClickListener(e:MouseEvent):void{
			
			Alert.show("Saving!! the image.\rPlease wait.");
			
			var bd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
			
			if(_directPos=="hgL") {
				bd.draw(_pa._ch);				
			} else if(_directPos=="hgR"){
				bd.draw(_pa._chR);				
			}
			bd.draw(_pa._ch);
			 
			//uploader.enocdeJPEG(bd); 
			
		}// end saveClickListener
		
		public function clearView():void {
			cont_sp.removeChildren();
			handler.setTarget(null);
		}
		
		
		
	}// end class
}// end package